<?php

class Keyword extends Parameter_model {
    
    function getList($filter = array(), &$pager=false)
	{
		$keyword = new Keyword();
        $keyword->joinAdd(new Video_Provider());
		if (isset($filter['name']) && $filter['name']) {
			$keyword->whereAdd(DRX_StringUtils::getI18nRealValueSql('KEYWORD.NAME')." LIKE '%".mysql_escape_string($filter['name'])."%'");
		}
		$keyword->selectAdd();
        $keyword->selectAdd('KEYWORD.*, VIDEO_PROVIDER.NAME PROVIDER');
		$keyword->orderBy(DRX_StringUtils::getI18nRealValueSql("KEYWORD.NAME"));
		if ($pager == true) {
			$pager = new DBPager($keyword, $filter, true);
		}
        $keyword->find();

		return $keyword;
	}
    
	public function isExistsByName($id = 0, $name= '', $lang_code = '')
	{
		if ($name == '')
		{
			return false;
		}	
		$keyword = new Keyword();
		if ($id) {
			$keyword->whereAdd("ID <> {$id}");
		}
        if ($lang_code) {
            $slq = " NAME LIKE '%<{$lang_code}>".trim($name)."</{$lang_code}>%'";
        } else {
            $slq = DRX_StringUtils::isI18nSqlCheckStringUnique($name, 'NAME');
        }
		$keyword->whereAdd($slq);
        $keyword->find();
		return $keyword->N > 0;
	}
    
    public function isExistsByIds($ids = 0)
	{
		if (!is_array($ids))
		{
			return false;
		}	
		$keyword = new Keyword();
		$keyword->whereAdd("ID IN (".  implode(",", $ids).")");
        $keyword->find();
		return $keyword->N == count($ids)?true:false;
	}

	public function validateInput($name_arr){
		global $error_msg;

        if (!count($name_arr)) {
            $error_msg->add(_MSG_INVALID_KEYWORD_NAME);
        } else {
            if (!isset($name_arr[REQ_LANG])) {
                $message = _LB_KEYWORD_NAME._MSG_VALUE_NAME_BY_LANGUAE_MISSING;
                $message = str_replace("[LANGUAGE]", "&lt;en&gt;", $message);
                $error_msg->add($message);
            }
            
            $name = "";
            foreach($name_arr as $code => $value) {
                $message = _MSG_VALUE_NAME_BY_LANGUAE_INVALID;
                $message = str_replace("[LANGUAGE]", "&lt;$code&gt;", $message);
                if (!textvalid($value))
                {
                    $error_msg->add($message);
                }

                if (self::isExistsByName($this->ID, $value, $code))
                {
                    $message = _MSG_VALUE_NAME_BY_LANGUAGE_EXISTS;
                    $message = str_replace("[LANGUAGE]", "&lt;$code&gt;", $message);
                    $error_msg->add($message);
                }

                $name .= "<".$code.">".$value."</".$code.">";
            }

            $this->NAME = $name;
        }
        
        if (!textvalid($this->ID_PROVIDER)) {
			$error_msg->add(_MSG_PROVIDER_NOT_EMPTY);
		} else {
            $video_provider = new Video_Provider();
            if (!$video_provider->get($this->ID_PROVIDER)) {
                $error_msg->add(_MSG_PROVIDER_INVALID);
            }
        }
		
		if (!textvalid($this->TYPE)) {
			$error_msg->add(_MSG_TYPE_NOT_EMPTY);
		}
                
//        if (self::isExistsByPosition($this->POSITION, $this->ID))
//		{
//			$error_msg->add(_MSG_POSITION_EXISTS);
//		}
        
		return $error_msg->isNoError();
	}
    
    function isExistsByPosition($position= 0, $id = 0)
	{
		$keyword = new Keyword();
		if ($id) {
			$keyword->whereAdd("ID <> {$id}");
		}

		$keyword->POSITION = $position;
        $keyword->find();
		
		return $keyword->N > 0;
	}
}